#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
分析bert_train表中的实际数据结构
用于诊断微调过程中出现的错误
"""

import json
from database_manager import DatabaseManager

def analyze_train_data():
    """
    分析训练数据结构
    """
    print("分析bert_train表中的实际数据结构...")
    
    # 创建数据库管理器实例
    db_manager = DatabaseManager()
    
    # 连接数据库
    if not db_manager.connect():
        print("错误: 无法连接到数据库")
        return
    
    # 获取训练数据
    print("获取训练数据...")
    train_data = db_manager.get_bert_train_data(10)  # 获取10条记录进行分析
    
    print(f"\n共获取到 {len(train_data)} 条训练数据")
    
    # 分析每条记录的结构
    for i, data in enumerate(train_data):
        print(f"\n--- 记录 {i+1} ---")
        print(f"ID: {data['id']}")
        print(f"Field ID: {data['field_id']}")
        print(f"原文: {data['original_text']}")
        print(f"Entity Info 类型: {type(data['entity_info'])}")
        print(f"Entity Info 值: {data['entity_info']}")
        
        # 检查entity_info是否为字符串
        if isinstance(data['entity_info'], str):
            print("  注意: entity_info是字符串类型")
            try:
                # 尝试解析为JSON
                parsed = json.loads(data['entity_info'])
                print(f"  解析为JSON后类型: {type(parsed)}")
                print(f"  解析为JSON后内容: {parsed}")
            except json.JSONDecodeError as e:
                print(f"  无法解析为JSON: {e}")
        elif isinstance(data['entity_info'], dict):
            print("  entity_info是字典类型")
        elif isinstance(data['entity_info'], list):
            print("  entity_info是列表类型")
        else:
            print(f"  entity_info是其他类型: {type(data['entity_info'])}")
    
    # 断开连接
    db_manager.disconnect()
    print("\n数据库连接已断开")

def test_data_processing():
    """
    测试数据处理逻辑
    """
    print("\n测试数据处理逻辑...")
    
    # 模拟从数据库获取的数据
    sample_data = [
        {
            'id': 1,
            'field_id': 1,
            'original_text': '承德钒钛钳工需要50MPa的压力容器操作证',
            'entity_info': '[{"nerKey": "组织", "nerValue": "承德钒钛"}, {"nerKey": "工种", "nerValue": "钳工"}]'
        },
        {
            'id': 2,
            'field_id': 1,
            'original_text': '河钢集团实施人工智能+行动工作方案',
            'entity_info': [{"nerKey": "单位", "nerValue": "河钢集团"}, {"nerKey": "方案", "nerValue": "行动工作方案"}]
        }
    ]
    
    print("处理模拟数据...")
    for i, data in enumerate(sample_data):
        print(f"\n处理记录 {i+1}:")
        print(f"  原始entity_info类型: {type(data['entity_info'])}")
        print(f"  原始entity_info值: {data['entity_info']}")
        
        # 检查并处理entity_info
        entity_info = data['entity_info']
        if isinstance(entity_info, str):
            try:
                entity_info = json.loads(entity_info)
                print(f"  解析后entity_info类型: {type(entity_info)}")
                print(f"  解析后entity_info值: {entity_info}")
            except json.JSONDecodeError as e:
                print(f"  解析JSON时出错: {e}")
                entity_info = []
        elif not isinstance(entity_info, list):
            print(f"  entity_info不是列表类型，转换为空列表")
            entity_info = []
        
        # 验证列表中的每个元素
        if isinstance(entity_info, list):
            for j, entity in enumerate(entity_info):
                print(f"    实体 {j+1}: {entity}")
                if isinstance(entity, dict):
                    print(f"      nerKey: {entity.get('nerKey', 'N/A')}")
                    print(f"      nerValue: {entity.get('nerValue', 'N/A')}")
                else:
                    print(f"      实体不是字典类型: {type(entity)}")

if __name__ == "__main__":
    analyze_train_data()
    test_data_processing()